<template>
  <Header :title="{ zh: '原材料仓目视管理看板', en: 'Raw Material Warehouse Visual Board', index: 1 }" />

  <div class="EchartsContainer">
    <div class="row">
      <div class="item">
        <div class="title">材料入库</div>
        <div class="echarts" id="main"></div>
      </div>
      <div class="item">
        <div class="title">材料出库</div>
        <div class="echarts" id="main2"></div>
      </div>
      <div class="item">
        <div class="title">订单完成数量</div>
        <div class="echarts" id="main3"></div>
      </div>
    </div>

    <div class="row" style="margin-top: 10px">
      <div class="item" style="margin-right: 10px; width: 55%">
        <div class="title" style="text-align: start">当月订单欠数状况 </div>
        <el-table border ref="tab" class="table" :data="showData.tableData" style="height: 100%">
          <el-table-column align="center" prop="lotNo" label="指令号" width="110" />
          <el-table-column align="center" prop="photoPath" label="鞋图">
            <template v-slot="{ row, $index }">
              <el-image hide-on-click-modal preview-teleported style="height: 34px" :src="row.photoPath"
                :zoom-rate="1.2" @click="imgClicked(row.photoPath, srcList)" :preview-src-list="srcList"
                :initial-index="imgIndex" fit="cover" />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="brand" label="品牌" />
          <el-table-column align="center" prop="orderQty" label="订单数量" width="100" />
          <el-table-column align="center" prop="mtlName" label="材料名称" width="160" />
          <el-table-column align="center" prop="mtlUnit" label="单位" width="65" />
          <el-table-column align="center" prop="shortQty" label="欠数" width="65" />
        </el-table>
      </div>
      <div class="item" style="width: 45%">
        <div class="title" style="text-align: start">材料库存状况</div>
        <el-table border class="table" ref="tab2" :data="showData.tableData2" style="height: 100%">
          <el-table-column align="center" prop="mtlNo" label="料号" />
          <el-table-column align="center" prop="mtlName" label="材料名称" width="150" />
          <el-table-column align="center" prop="mtlUnit" label="单位" width="70" />
          <el-table-column align="center" prop="stkQty" label="库存数" width="120" />
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
import mtlApi from '@/api/material'
import generateRandomArray from '@/utils/radom'
let rollTimer = ref() as any

const srcList = ref([]);
const imgIndex = ref(0);
// 点击图片
const imgClicked = (src: any, srcList: any) => {
  let index = srcList.findIndex((item: any) => item === src);
  imgIndex.value = index;
};
const tab = ref<HTMLElement>() as any
const tab2 = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  // 这里的 tab 是上方 table 表格绑定的ref值
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  const divData2 = tab2.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    // + 4 是每秒向下滑动 4个像素  这块可以自己更改
    divData.scrollTop += 1
    divData2.scrollTop += 1
    // console.log(divData.scrollTop)
    // 下方判断是滑动到底部就会自己回到最上方重新开始滑动  改动像素的话 一定要满足进入这个判断  否则滚动到底部就停了
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
    if (divData2.clientHeight + divData2.scrollTop == divData2.scrollHeight) {
      divData2.scrollTop = 0
    }
  }, 150)
}
onMounted(() => {
  autoRoll(false)
})
onBeforeUnmount(() => {
  autoRoll(true)
})

const showData = ref({
  chart1: {
    data: [
      {
        name: '正单入库',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [12028.4, 11681.2, 13087.8, 8803.3, 0, 0]
      },
      {
        name: '补单入库',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0, 0, 0, 0, 0]
      },
    ]
  },
  chart2: {
    data: [
      {
        name: '正单出库',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [4229.5, 6511.6, 4036.3, 2199, 0, 0]
      },
      {
        name: '补单出库',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0, 0, 0, 0, 0]
      },
    ]
  },
  chart3: {
    xData: [],
    data: [
      {
        name: '接单数量',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [301564, 213169, 305543, 259644]
      },
      {
        name: '已配数量',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [299542, 85470, 3596, 1582]
      }, {
        name: '欠数数量',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [2022, 127699, 301947, 259644]
      },
    ]
  },
  tableData: [],
  tableData2: []
}) as any
const getData = async () => {

  // 获取表格
  // try {
  //   let tableDataList = await request.post('/mtl/inDetail', {
  //     pageNum: '1',
  //     pageSize: '200'
  //   }) as any
  //   let tableDataList2 = await request.post('/mtl/stk/query', {
  //     pageNum: '1',
  //     pageSize: '200'
  //   }) as any
  //   showData.value.tableData = tableDataList.dto
  //   showData.value.tableData2 = tableDataList2.dto
  // } catch (error: any) {
  //   ElMessage.error(error)
  // }
}

onMounted(async () => {
  // await getDeptTypes()
  await getData()
  getEchartsDom()
})
const getEchartsDom = () => {
  // 第一张图
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let app = {} as any
  const posList = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0,
      max: 100
    }
  }
  app.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app.config.rotate,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        position: app.config.position,
        distance: app.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  let option = {
    color: ['#FAC858', '#93BEFF', '#507AFC', '#FF7700', '#91CC75', '#FF696E'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: 40,
      right: 0,
      top: 30,
      bottom: 45
    },
    legend: {
      data: ['正单入库', '补单入库'],
      top: 'bottom'
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: showData.value.chart1.data
  }
  option && myChart.setOption(option)

  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let app2 = {} as any
  const posList2 = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app2.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList2.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0,
      max: 100
    }
  }
  app2.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app2.config.rotate,
        align: app2.config.align,
        verticalAlign: app2.config.verticalAlign,
        position: app2.config.position,
        distance: app2.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  let option2 = {
    color: ['#FAC858', '#93BEFF', '#507AFC', '#FF7700', '#91CC75', '#FF696E'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: 40,
      right: 0,
      top: 30,
      bottom: 45
    },
    legend: {
      data: ['正单出库', '补单出库'],
      top: 'bottom'
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: showData.value.chart2.data
  }
  option2 && myChart2.setOption(option2)

  // 第三张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let app3 = {} as any
  const posList3 = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app3.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList3.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0,
      max: 100
    }
  }
  app3.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app3.config.rotate,
        align: app3.config.align,
        verticalAlign: app3.config.verticalAlign,
        position: app3.config.position,
        distance: app3.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  let option3 = {
    color: ['#FAC858', '#93BEFF', '#507AFC', '#FF7700', '#91CC75', '#FF696E'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    legend: {
      data: ['接单数量', '已配数量', '欠数数量'],
      top: 'bottom'
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['8月', '9月', '10月', '11月']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: showData.value.chart3.data
  }
  option3 && myChart3.setOption(option3)
}

onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  height: calc(100vh - 7vh - 40px);
  width: 100%;
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: 0 10px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

.table {
  margin-top: 5px;
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
